//app.js
App({
  onLaunch: function () {
    var self = this;
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    /**
     * 拿到code,然后获取用户信息,
     * 用户信息获取到以后,和code 一起去后台换取jwt
     * 保存jwt, 把 jwt 作为用户登录凭据
     */
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res); 
        var code = res.code;
        wx.getUserInfo({
          success: function (res) {
            console.log(res);
            wx.request({
              method: 'post',
              data: {
                userinfo:res.userInfo,
                code : code
              },
              url: getApp().globalData.baseUrl + '/index.php?r=v1/account/index',
              success: function (res) {
                console.log(res);
                if(res.data.code==200)
                {
                  wx.setStorage({
                    key: 'bearer',
                    data: res.data.data.bearer,
                  })
                }
                else
                {
                  wx.showToast({
                    title: res.data.msg,
                  })
                }
              }
            });
          }
        });
        
      }
    });

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    });

    wx.getStorage({
      key: 'bearer',
      success: function(res) {
        self.globalData.bearer = res.data;
        console.log('load bearer from storage');
        console.log(res);
      },
    })
  },
  // 获取api URL
  getApiUrl: function(api){
    return this.globalData.baseUrl + api;
  },
  //发送HTTP GET请求
  httpGet: function(api,callback){
    var self = this;
    wx.request({
      url: self.getApiUrl(api),
      success: function (res) { callback.success(res.data) },
      header: { Authorization: "bearer " + self.globalData.bearer}
    });
  },
  //发送HTTP POST请求
  httpPost: function (api, callback, params){
    var self = this;
    wx.request({
      url: self.getApiUrl(api),
      method: "POST",
      success: function (res) { callback.success(res.data) },
      header: { Authorization: "bearer " + self.globalData.bearer },
      data: params
    });
  },
  //发送HTTP PUT请求
  httpPut: function (api, callback, params){
    var self = this;
    wx.request({
      url: self.getApiUrl(api),
      method: "PUT",
      success: function (res) { callback.success(res.data) },
      header: { Authorization: "bearer " + self.globalData.bearer },
      data: params
    });
  },
  //发送HTTP DELETE请求
  httpDelete: function (api, callback){
    var self = this;
    wx.request({
      url: self.getApiUrl(api),
      success: function (res) { callback.success(res.data) },
      header: { Authorization: "bearer " + self.globalData.bearer },
      method:'DELETE'
    });
  },
  toast: function(title)
  {
    wx.showToast({
      title: title,
      icon: 'none'
    })
  },
  success: function (title) {
    wx.showToast({
      title: title,
      icon: 'success'
    })
  },
  //https://sh.magentozh.cn
  //http://shuhua.localdev.cn
  //#B52F32
  globalData: {
    userInfo: null,
    baseUrl: "https://sh.magentozh.cn",
    timelineTab:0,
    bearer : '',
    customer: {}
  }
})